<?php
require_once(CORE_DIR.'Common.php');
require_once(CORE_DIR.'mappers/PermissionsMapper.php');

class permissionModel extends Model{

	function __construct(){
		$this->conn = $this->getConnection();
	}
	
	function getAllPermissions()
	{
		$query = "SELECT * FROM permissions";
		$result = mysqli_query($this->conn, $query);
		
		return $this->execute($query, new PermissionsMapper());
	}
	
	function getProjectPermissions($projectId)
	{
		$query = "SELECT * FROM permissions where projectID=%d";		
		$query = $this->format($query, array($projectId));
		
		return $this->execute($query, new PermissionsMapper());
	}
	
	function addPermission($projectId, $userID, $permission){
		$query = "insert into permissions (projectID, userID, permission)
			  values (%d, %d, %d)";
		$query = $this->format($query, array($projectId, $userID, $permission));		
		return $this->execute($query, null);
	}
	
	function deleteProjectPermissions($projectId){
		$query = "delete from permissions where projectID=%d";
		$query = $this->format($query, array($projectId));
		return $this->execute($query, null);
	}
	
	function addPermissionsToProj($projectId, $permissionParam){
		$userPermList = explode(";", $permissionParam);
		for ($i = 0; $i < count($userPermList); $i++)
		{
			$userPerm = explode("|", $userPermList[$i]);
			$query = "insert into permissions (projectID, userID, permission)
				  values (%d, %d, %d)";
			$query = $this->format($query, array($projectId, $userPerm[0], $userPerm[1]));		
			$this->execute($query, null);
		}		
	}	
}
?>
